package com.wanxi.servlet.customer;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CustomerModel;
import com.wanxi.service.CustomerService;
import com.wanxi.service.Impl.CustomerServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/5 20:16
 */
@WebServlet("/back/FindCustomerById")
public class FindCustomerByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.获取前端数据
        String id = req.getParameter("id");
        //3.封装model
        CustomerModel customerModel = CustomerModel.builder()
                .id(Tool.nullToInt(id))
                .build();
        //4.调用service层
        CustomerService customerService = new CustomerServiceImpl();
        ResultDto resultDto = customerService.findById(customerModel);
        //5.返回前台数据
        resp.getWriter().println(JSONObject.toJSONString(resultDto));

    }
}
